package com.wh.productMag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.stock.dao.StockMapper;
import com.wh.stock.pojo.Stock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wh.productMag.pojo.ProductList;
import com.wh.productMag.dao.ProductListMapper;
import com.wh.productMag.service.IProductListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wh.productMag.dto.ProductListQueryDto;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 业务实现类
 */
@Service
public class ProductListServiceImpl extends ServiceImpl<ProductListMapper, ProductList> implements IProductListService {

    @Autowired
    private ProductListMapper productListMapper;
    @Autowired
    private StockMapper stockMapper;


    @Override
    public IPage<ProductList> findByPage(ProductListQueryDto productListQueryDto){
        //1.创建查询条件
        QueryWrapper<ProductList> qw = new QueryWrapper<>();
        //qw.like("xxx",productListQueryDto.getXxx());
        //qw.or();
        //qw.like("xxx",productListQueryDto.getXxx());
        //2.组件分页数据
        IPage<ProductList> page = new Page<>(productListQueryDto.getCurrentPage(), productListQueryDto.getPageSize());
        page.setRecords(productListMapper.findByPage(page,qw));
        page.setTotal(productListMapper.selectCount(qw));
        //3.返回
        return page;
    }

    @Override
    public void offShelf(Integer id) {
        Integer state=1;

        productListMapper.offShelf(id,state);
    }

    @Override
    @Transactional
    public void addData(ProductList productList) {
        String productName = productList.getProductName();//产品名字
        String picture = productList.getPicture();//主图片
        BigDecimal price = productList.getPrice();
        productList.setState(0);
        Stock stock = new Stock();
        stock.setName(productName);
        stock.setPicture(picture);
        stock.setClassifyId(productList.getClassifyId());
        stock.setUpdateTime( LocalDateTime.now());
        stock.setPrice(price);
        stock.setStock(productList.getStock());
        //库存中先添加信息
        stockMapper.insert(stock);
        //将新添加的库存关联产品
        productList.setStockId(stock.getId());
        //添加数据
        productListMapper.insert(productList);
    }

    @Override
    public void deletebyid(Integer id) {
        Integer state=2;
        productListMapper.deletebyid(id,state);
    }

    @Override
    public List<ProductList> getproduct(Long id) {
      return  productListMapper.findproduct(id);
    }
    @Override
    public Integer allNum() {
        return productListMapper.selectCount(null);
    }

    @Override
    public IPage<ProductList> forSale(ProductListQueryDto productListQueryDto) {
        //1.创建查询条件
        QueryWrapper<ProductList> qw = new QueryWrapper<>();
        qw.eq("state",0).like("product_name",productListQueryDto.getKeyword());
        //qw.like("xxx",productListQueryDto.getXxx());
        //qw.or();
        //qw.like("xxx",productListQueryDto.getXxx());
        //2.组件分页数据
        IPage<ProductList> page = new Page<>(productListQueryDto.getCurrentPage(), productListQueryDto.getPageSize());
        page.setRecords(productListMapper.findByPage(page,qw));
        page.setTotal(productListMapper.selectCount(qw));
        //3.返回
        return page;
    }

    @Override
    public void updateProduct(ProductList productList) {
        //产品表更新
        productListMapper.updateById(productList);
        //库存表信息更新
        String productName = productList.getProductName();//产品名字
        String picture = productList.getPicture();//主图片
        BigDecimal price = productList.getPrice();//价格
        Integer classifyId = productList.getClassifyId();
        Integer stockId = productList.getStockId();
      /**
       * 库存管理更新
       */
        stockMapper.updateProduct(stockId,productName,picture,price,classifyId);
    }
}
